package com.myjpa.generate.wdl;

import com.myjpa.generate.GenerateContext;
import com.myjpa.generate.Generator;
import com.myjpa.generate.JdbcGeneratorUtils;
import org.junit.Test;

public class TaskGen {

    boolean override = false;
    String[] org_tables_name_old = {"org_account",
            "org_application",
            "org_auth_company_prj",
            "org_auth_dept_prj",
            "org_auth_prj_data",
            "org_cfg_dept_job",
            "org_cfg_group_app",
            "org_cfg_group_func",
            "org_cfg_job_app",
            "org_cfg_job_func",
            "org_cfg_person_company",
            "org_cfg_person_dept",
            "org_cfg_person_job",
            "org_cfg_person_prj",
            "org_cfg_prj_app",
            "org_cfg_prj_func",
            "org_company",
            "org_dept",
            "org_dept_lv_template",
            "org_dept_template",
            "org_function",
            "org_function_lv",
            "org_group",
            "org_job",
            "org_job_dept_template",
            "org_job_template",
            "org_person",
            "org_product",
            "org_rel_company_prj"};

    String[] org_tables_name2 = {"org_company_project_rel"};
    String[] org_tables_name1 =
            {"org_application",
                    "org_company",
                    "org_dept",
                    "org_dept_classify",
                    "org_function",
                    "org_group",
                    "org_job",
                    "org_job_classify",
                    "org_job_classify_rel",
                    "org_user",
                    "org_project"};
    String[] alarm_tables_name =
            {"alarm_log",
            "alarm_comment"};

    String[] org_tables_name = {"org_group_app_rel", "org_group_function_rel"};

    private GenerateContext initGenerateContext() {
        GenerateContext ctx = new GenerateContext();
        String path = "/Users/king/work/persagy-dmp/dmp-org";
        ctx.setRootPath(path);
//        ctx.setRootPath("/Users/devsaga/workspace/idea/myjpa/myjpa-querydsl-5");
        /** 不需要改 */
        ctx.setResponsePackage("com.persagy.common.web");
        ctx.setCriteriaPackage("com.persagy.common.criteria");
        ctx.setPkg("com.persagy.dmp.org");
        ctx.setOverrideFile(override);
        return ctx;
    }

    @Test
    public void initClassDef() {


        for (String otn : org_tables_name) {
            Generator generator = new Generator();
            GenerateContext ctx = initGenerateContext();
            // 配置主键及生成策略
            ctx.setPrimaryKey("id", "String", GenerateContext.PrimaryKey.GENERATION_TYPE_ASSIGNED);
            ctx.setClassName(subName(otn, true));
            ctx.setTableName(otn);
            ctx.setMappingPath(subName(otn, false));
            ctx.setOverrideFile(false);
            ctx.setWithModel(true); // 生成model类

            // 从数据库解析字段
            String url = "jdbc:mysql://localhost:3306/dmp_test";
            String username = "root";
            String password = "root";
            // 数据库名
            String tableSchema = "dmp_test";
//            String url = "jdbc:mysql://39.102.54.110:9005/dmp_test";
//            String username = "persagy";
//            String password = "persagy@020";
//            String tableSchema = "dmp_test";
            JdbcGeneratorUtils.initAttribute(url, username, password, tableSchema, ctx);

            generator.generate(ctx);
        }

    }

    @Test
    public void testSub(){
        String name = "org_group_app_rel";
        String s = subName(name, true);
        System.out.println(s);
        String s1 = subName(name, false);
        System.out.println(s1);
    }

    private String subName(String name, Boolean isClassName){
        String[] s = name.split("_");
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i < s.length; i++) {
            if (isClassName){
                stringBuffer.append(conWord(s[i]));
            } else {
                stringBuffer.append("/").append(s[i]);
            }
        }

        return stringBuffer.toString();
    }

    private String conWord(String word){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(word.substring(0, 1).toUpperCase()).append(word.substring(1));
        return stringBuffer.toString();
    }

    @Test
    public void testconversion(){
        String web = conWord("web");
        System.out.println(web);
    }
}
